package com.nime.novel.crawl.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author Administrator
 */
@Slf4j
public class HttpUtil {

    private static RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");


    public static String getByHttpClient(String url) {
        String html = null;
        for (int i = 0; i<3; i++) {
            try {
                ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
                if (forEntity.getStatusCode() == HttpStatus.OK) {
                    html = forEntity.getBody();
                }
            } catch (Exception e) {
                log.info("e -> {}", e.getMessage());
            }
            if (StringUtils.isNotEmpty(html)) {
                break;
            }
        }
        return html;
    }
}
